package ibm.websphere.monitor.action;

import com.opensymphony.xwork2.ActionSupport;
import ibm.websphere.monitor.bean.UserInfo;
import ibm.websphere.monitor.util.Constants;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *	@Description 其他ACTION的基类//
 * </pre>
 * @copyright www.timotai.net
 * @author 这家伙真懒
 * @date 2016-1-12
 **/

public class BaseAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{

	private static final long serialVersionUID = 1L;

	/**初始化 request 对象**/
	protected HttpServletRequest request =null;

	/**初始化 response 对象**/
	protected HttpServletResponse response = null;

	/**是否需要DWR 支持，默认为 false**/
	private boolean requireDWR = false;

	/**ACTION 要跳转的页面**/
	private String target;

	/**是否需要绑定grid控件**/
	private boolean initGrid;

	/**是否需要日期控件**/
	private boolean requireDate;

	/**保存需要加载的脚本文件**/
	private List<String> _scripts = new ArrayList<String>();


	public boolean isRequireDate(){
		return requireDate;
	}

	public void setRequireDate(boolean requireDate){
		this.requireDate = requireDate;
	}

	public boolean isInitGrid(){
		return initGrid;
	}

	public void setInitGrid(boolean initGrid){
		this.initGrid = initGrid;
	}

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}



	/**
	 * <pre>
	 * 增加对DWR的支持
	 * </pre>
	 * @param js
	 */
	public void addDWR(String js)
	{
		this.requireDWR = true;
		if(this._scripts.contains("dwr/interface/" + js + ".js"))
		{
			request.setAttribute("WAS_PLAT_SCRIPTS", _scripts);
			request.setAttribute("WAS_REQUER_DWR", requireDWR);
			return ;
		}
		this._scripts.add("dwr/interface/" + js + ".js");
		request.setAttribute("WAS_PLAT_SCRIPTS", _scripts);
		request.setAttribute("WAS_REQUER_DWR", requireDWR);
	}


	/**
	 * <pre>
	 * 	ACTION 的返回值处理 针对页面
	 * </pre>
	 * @param view 跳转的页面
	 * @return
	 */
	protected String redirect(String view){

		setTarget(Constants.WAS_PLAT_DEFAULT_PAGE);

		request.setAttribute(Constants.WAS_PLAT_FORWARD_PAGE_ATTR,view);

		return Constants.DEFAULT_VIEW;
	}

	/**
	 * <pre>
	 * 	设置返回的URL
	 * </pre>
	 * @param url
	 */
	public void returnUrl(String url){
		request.getSession().setAttribute(Constants.WAS_RETURN_URL, url);
	}

	/**
	 * <pre>
	 * 	ACTION 的返回值处理 针对ACTION
	 * </pre>
	 * @param type
	 * @param target 跳转的ACTION
	 * @return
	 */
	protected String redirect(String type,String target){
		setTarget(target);
		return type;
	}

	/**
	 * <pre>
	 * 	对request对象初始化
	 * </pre>
	 * @param request
	 */
	public void setServletRequest(HttpServletRequest request) {
		this.request =  request;
		//HttpSession session = request.getSession();
	}

	/**
	 * <pre>
	 * 	对response对象初始化
	 * </pre>
	 * @param response
	 */
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	/**
	 * 根据参数获取reqeust中的值
	 * @param pName 参数名称
	 * @return value值
	 */
	protected String getParam(String pName){
		String v = request.getParameter(pName);
		return v == null ? "" : v;
	}

	/**
	 * 获取当前用户对象
	 * @return UserInfo
	 */
	protected UserInfo getNowUser(){
		return (UserInfo)request.getSession().getAttribute(Constants.SESSION_USER);
	}

	/**
	 * <pre>
	 * 	从SESSION中获取用户对象
	 * </pre>
	 * @return
	 */
	public UserInfo getLoginUser() {
		return  (UserInfo)request.getSession().getAttribute(Constants.SESSION_USER);
	}

	/**
	 * <pre>
	 * 	获取当前目录名称
	 * </pre>
	 * @return
	 */
	public String getContextPath(){
		return request.getContextPath();
	}

	/**
	 * <pre>
	 * 	设置操作成功的返回页面
	 * </pre>
	 */
	public void getReturnRoleUrl(){
		returnUrl(getContextPath()+"/RoleManage/roleList");
	}

	@SuppressWarnings("deprecation")
	public String getRealPath(){
		return request.getRealPath("/").replaceAll("\\\\", "/");
	}

}
